#! /bin/bash

create_file_with_content() {
    [[ -d $(dirname "$1") ]] || mkdir -p "$(dirname "$1")"
    echo "$2" >"$1"
}

# mock the installation of MATLAB in the Docker container

DIR=$(dirname "$0")
if [[ -f "${DIR}/FAIL" ]]; then
    cat ${DIR}/FAIL >&2
    exit 1
fi

MATLABLOG=matlab.log

MATLAB_BIN=/usr/local/MATLAB/bin/matlab


create_file_with_content "$MATLAB_BIN" "$(
    cat <<-EOM
#!/bin/sh

# mock function to mock the call to matlab

# creates the file $MATLABLOG and writes "MATLAB" followed by the extra
# arguments passed to the "matlab" command
echo "MATLAB \$@" | tee $MATLABLOG

# After writing on ${MATLABLOG} start a bash CLI, to keep the docker container
# running
/bin/bash
EOM
    )"

chmod +x $MATLAB_BIN